package com.first.xiinn.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.first.core.dao.impl.BaseDaoImpl;
import com.first.core.util.PagingBean;
import com.first.xiinn.dao.ShortMessageDao;
import com.first.xiinn.model.ShortMessage;

public class ShortMessageDaoImpl extends BaseDaoImpl<ShortMessage>
  implements ShortMessageDao
{
  public ShortMessageDaoImpl()
  {
    super(ShortMessage.class);
  }

  public List<ShortMessage> findAll(String userId, PagingBean pb)
  {
    String hql = "from ShortMessage vo where vo.senderId=?";
    Object[] objs = { userId };
    return findByHql(hql, objs, pb);
  }

  public List<ShortMessage> findByUser(String userId)
  {
    String hql = "from ShortMessage vo where vo.senderId=?";
    Object[] objs = { userId };
    return findByHql(hql, objs);
  }

  public List searchShortMessage(String userId, ShortMessage shortMessage, Date from, Date to, PagingBean pb)
  {
    ArrayList paramList = new ArrayList();
    StringBuffer hql = new StringBuffer("select vo1,vo2 from InMessage vo1,ShortMessage vo2 where vo1.shortMessage=vo2 and vo1.delFlag=0 and vo1.userId=?");
    paramList.add(userId);
    if (shortMessage != null) {
      if (StringUtils.isNotEmpty(shortMessage.getSender())) {
        hql.append(" and vo2.sender=?");
        paramList.add(shortMessage.getSender());
      }
    }
    if (to != null) {
      hql.append("and vo2.sendTime <= ?");
      paramList.add(to);
    }
    if (from != null) {
      hql.append("and vo2.sendTime >= ?");
      paramList.add(from);
    }
    hql.append(" order by vo2.sendTime desc");
    return findByHql(hql.toString(), paramList.toArray(), pb);
  }
}

